package com.xp.tinyurl.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;

@Slf4j
public class XmlUtils {

    /**
     *
     * @param xmlContent
     * @param nodeName
     * @return
     */
    public static String getTextByNodeName(String xmlContent, String nodeName){
        //1.创建DocumentBuilderFactory对象
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //2.创建DocumentBuilder对象
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document d = builder.parse(new ByteArrayInputStream(xmlContent.getBytes()));
            NodeList casMail = d.getElementsByTagName(nodeName);
            return casMail.item(0).getTextContent();
        } catch (Exception e) {
            log.error("解析xml内容异常", e);
        }
        return null;
    }

}
